package com.ita.os.client.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.os.client.global.GlobalVar;
import com.ita.os.client.po.Dish;
import com.ita.os.client.po.Merchant;
import com.ita.os.client.po.Page;
import com.ita.os.client.service.DishService;
import com.ita.os.client.service.MerchantService;
import com.ita.os.client.service.impl.DishServiceImpl;
import com.ita.os.client.service.impl.MerchantServiceImpl;
import com.ita.os.client.util.PageUtil;
import com.ita.os.client.util.StringUtil;

/**
 * Servlet implementation class LoadDishServlet
 */
@WebServlet("/loadDish")
public class LoadDishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DishService dishService = new DishServiceImpl();
	private MerchantService merchantService = new MerchantServiceImpl();
    public LoadDishServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int mid = Integer.parseInt(request.getParameter("mid"));
		Merchant m = merchantService.merchantInfo(mid);
		String curPage = request.getParameter("curPage");
		if(StringUtil.isEmpty(curPage)){
			curPage="1";
		}
		Page p=new Page(Integer.parseInt(curPage),20);
		List<Dish> dList = dishService.listDishes(p, mid);
		int total=dishService.countDishes(mid);
		String pageCode=PageUtil.getPagation(request.getContextPath()+"/loadDish?mid="+ mid + "&", total, Integer.parseInt(curPage), 20);
		request.setAttribute("imgPath", GlobalVar.imgPath);
		request.setAttribute("dList", dList);
		request.setAttribute("merchant", m);
		request.setAttribute("pageCode", pageCode);
		request.getRequestDispatcher("/menu.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
